﻿namespace HelperFromTheSkeleton.Model.RequestModels
{
    public class ResultModel
    {
        public string? Message { get; set; }

        public bool IsSucceed { get; set; }

        public static ResultModel Succeed(string message)
        {
            return new ResultModel
            {
                Message = message,
                IsSucceed = true
            };
        }

        public static ResultModel Fall(string message)
        {
            return new ResultModel
            {
                Message = message,
                IsSucceed = false
            };
        }

        public static ResultModel Fall(Exception? ex)
        {
            return new ResultModel
            {
                Message = (ex?.Message ?? "请联系管理员"),
                IsSucceed = false
            };
        }
    }

    /// <summary>
    /// 结果模型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ResultModel<T> : ResultModel
    {
        /// <summary>
        /// 数据
        /// </summary>
        public T? Data { get; set; }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static ResultModel<T> Succeed(T data, string message)
        {
            var result = new ResultModel<T>
            {
                Message = message,
                IsSucceed = true,
                Data = data
            };
            return result;
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static new ResultModel<T> Fall(string message)
        {
            var result = new ResultModel<T>
            {
                Message = message,
                IsSucceed = false,
                Data = default
            };
            return result;
        }
    }
}
